/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pfc.arbol;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;
import javax.swing.tree.TreeNode;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.ddlutils.model.Database;
import pfc.modeloer.ERModel;
import pfc.comun.IDEpfc;
import pfc.comun.Proyecto;
import pfc.gui.PropertiesEdit;
import pfc.utils.Utils;

/**
 *
 * @author xandru
 */
public class DBTreeNode extends PFCTreeNode implements ActionListener {

    public DBTreeNode(Proyecto p, String nombre) {
        super(p, nombre);
        
        construirMenuContexto();

    }


    @Override
    protected void construirMenuContexto() {
        super.construirMenuContexto();
        //popupMenu = new JPopupMenu();
        File f = new File(dsFile);

        /*
        JMenuItem mi = new JMenuItem("Obtener Modelo de Datos");
        mi.setActionCommand("getDataModel");
        mi.addActionListener(this);
        popupMenu.add(mi);
        mi.setEnabled(f.exists());

        
        mi = new JMenuItem("Verificar Modelo de datos");
        mi.setActionCommand("verifyDataModel");
        mi.addActionListener(this);
        popupMenu.add(mi);
        mi.setEnabled(f.exists());

        mi = new JMenuItem("Sincronizar Modelo de datos");
        mi.setActionCommand("syncDataModel");
        mi.addActionListener(this);
        popupMenu.add(mi);
        mi.setEnabled(f.exists());
        popupMenu.add(new JSeparator());

        mi = new JMenuItem("Obtener Entidades-Relaciones");
        mi.setActionCommand("getERModel");
        mi.addActionListener(this);
        popupMenu.add(mi);
        mi.setEnabled(f.exists());
         * 
         */
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("getDataModel")) {
            PropertiesConfiguration conf = null;
            try {
                conf = new PropertiesConfiguration(dsFile);
            } catch (ConfigurationException ex) {
                Logger.getLogger(DBTreeNode.class.getName()).log(Level.SEVERE, null, ex);
            }

            Database dbModel = Utils.getDatabaseModel(conf);
            Utils.modelToXML(dbModel, dbModelFile);


            DataModelTreeNode dmtn = new DataModelTreeNode(proyecto, "Modelo Datos");
            this.add(dmtn);
            IDEpfc.refreshTree(this);
        }


        if (e.getActionCommand().equals("getERModel")) {
            Database db = Utils.XMLtoModel(dbModelFile);
            ERModel er = new ERModel(db);

            ERModelTreeNode en = (ERModelTreeNode) findFirstChild(ERModelTreeNode.class);

            Utils.modelToXML(er, entityModelFile);
            if (en!=null)
                this.remove(en);

            en = new ERModelTreeNode(proyecto,"Modelo E-R");
            en.buildTree( er );
            this.add(en);
            IDEpfc.refreshTree(this);          
        }


        if (e.getActionCommand().equals("addDataSource")) {
            PropertiesEdit dsProperties = null;
            try {
                PropertiesConfiguration conf = new PropertiesConfiguration();
                conf.addProperty("driver", "");
                conf.addProperty("url", "");
                conf.addProperty("usuario", "");
                conf.addProperty("password", "");

                conf.save(dsFile);
                conf = new PropertiesConfiguration(dsFile);
                dsProperties = new PropertiesEdit(conf);
            } catch (ConfigurationException ex) {
                Logger.getLogger(DBTreeNode.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println(ex.getMessage());
            }
            IDEpfc.app.addParaEditar(dsProperties,"Origen de Datos");

            DataSourceTreeNode ds = new DataSourceTreeNode(proyecto, "Origen Datos");
            this.add(ds);
        }




        return;
    }


}
